﻿using AutoMapper;
using BS.Infra;
using BS.Infra.Interface;
using MediatR;

namespace BS.Read.Api.Handler.Breeding
{
    public class GetBreedingsByIdHandler : IRequestHandler<GetBreedsingByIdRequestDTO, ApiResult>
    {
        private readonly IBreedingsRepository _breedingsRepository;

        private readonly ILogger<GetBreedingsByIdHandler> _logger;

        private readonly IMapper _mapper;

        public GetBreedingsByIdHandler(IBreedingsRepository breedingsRepository,
            ILogger<GetBreedingsByIdHandler> logger,
            IMapper mapper)
        {
            _breedingsRepository = breedingsRepository;
            _logger = logger;
            _mapper = mapper;
        }
        public async Task<ApiResult> Handle(GetBreedsingByIdRequestDTO request, CancellationToken cancellationToken)
        {
            try
            {
                var result = await _breedingsRepository.GetAsync(x=>x.BreedingId== request.BreedingId);
                if (result.BreedingId == null)
                {
                    return ApiResult.Fail("回显失败");
                }
                return  ApiResult.Ok(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error in GetBreedingsByIdHandler");
                return ApiResult.Fail("Internal Server Error");
            }
        }
    }
}
public class GetBreedsingByIdRequestDTO:IRequest<ApiResult>
{
    public int BreedingId { get; set; }
}